﻿using SD.Entity;
using SD.Entity.Jsons;
using SD.Entity.PageSearch;
using SD.Entity.SearchEntity;
using SD.Interface.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SD.UI.Controllers
{
    public class ActionInfoController : Controller
    {
        //
        // GET: /ActionInfo/
        private IActionInfoService _actionInfoService;

        public ActionInfoController(IActionInfoService actionInfoService)
        {
            _actionInfoService = actionInfoService;
        }

        public ActionResult Index()
        {
            return View();
        }

        public JsonResult ActionInfoList(ActionInfoSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<ActionInfo> list = _actionInfoService.QueryActionInfoList(search, view);
            JsonQTable at = JsonQTable.ConvertFromPagedList<ActionInfo>(list, colkey, colsinfo.Split(','));
            return Json(at);
        }

        public ActionResult ActionInfoEdit(int? id)
        {
            ActionInfo actionInfo = null;
            if (id.HasValue)
            {
                actionInfo = _actionInfoService.GetActionInfo(id.Value);
            }
            else
            {
                actionInfo = new ActionInfo();
            }
            return View(actionInfo);
        }

        public JsonResult ActionInfoSave(ActionInfo actionInfo)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                actionInfo.SubTime = DateTime.Now;
                int ret = (int)_actionInfoService.SaveActionInfo(actionInfo);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Message = "操作成功！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        public ActionResult Test()
        {
            return null;
        }

    }
}
